﻿using System.Data;
using Marbles.Data;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;


namespace Marbles.CMS.ContentTypes.Blog
{
    public class ContentManager : ContentManagerBase
    {
        public List<BlogEntry> GetBlogEntries(PageDefinition Definition)
        {
            return GetBlogEntries(Definition.PageName);
        }


        public List<BlogEntry> GetBlogEntries(string PageName)
        {
            DataSource dbc = new DataSource(ContentSettings.connectionString);
            DataSourceCommand cmd = new DataSourceCommand("SELECT * FROM BlogEntry WHERE pagename = @pagename ORDER BY publishdate DESC");
            List<BlogEntry> items = new List<BlogEntry>();
            cmd.Parameters.Add("@pagename", PageName);
            DataTable dt = dbc.FetchDataTable(cmd);

            foreach (DataRow dr in dt.Rows)
            {
                BlogEntry item = new BlogEntry();
                item.Description = dr["description"].ToString();

                if (string.IsNullOrEmpty(dr["ident"].ToString())) { item.Guid = Guid.NewGuid(); } else { item.Guid = new Guid(dr["ident"].ToString()); }

                item.Index = Convert.ToInt32(dr["articlenumber"]);
                item.Link = new Uri(HttpContext.Current.Request.Url.ToString());
                item.PublishDate = Convert.ToDateTime(dr["publishdate"]);
                item.Title = dr["title"].ToString();
                items.Add(item);
            }

            return items;
        }

        public void UpdateBlog(PageDefinition Definition, List<BlogEntry> items)
        {
            DataSource dbc = new DataSource(ContentSettings.connectionString);
            UpdateDefinition(Definition);

            foreach (BlogEntry item in items)
            {
                DataSourceCommand cmditem = new DataSourceCommand("UPDATE BlogEntry SET author = @author, title = @title, description = @description, publishdate = @publishdate WHERE articlenumber = @articlenumber AND pagename = @pagename");
                cmditem.Parameters.Add("@title", item.Title);
                cmditem.Parameters.Add("@description", item.Description);
                cmditem.Parameters.Add("@author", item.Author);
                cmditem.Parameters.Add("@publishdate", item.PublishDate);
                cmditem.Parameters.Add("@pagename", Definition.PageName);
                cmditem.Parameters.Add("@articlenumber", item.Index);
                dbc.ExecuteCommand(cmditem);
            }
        }

        public void InsertBlog(PageDefinition Definition, List<BlogEntry> items)
        {
            DataSource dbc = new DataSource(ContentSettings.connectionString);
            int i = 1;
            InsertDefinition(Definition);

            foreach (BlogEntry item in items)
            {
                DataSourceCommand cmditem = new DataSourceCommand("INSERT INTO BlogEntry (pagename, ident, title, description, publishdate, author, articlenumber) VALUES (@pagename, @ident, @title, @description, @publishdate, @author, @articlenumber)");
                cmditem.Parameters.Add("@articlenumber", i);
                cmditem.Parameters.Add("@title", item.Title);
                cmditem.Parameters.Add("@description", item.Description);
                cmditem.Parameters.Add("@publishdate", item.PublishDate);
                cmditem.Parameters.Add("@author", item.Author);
                cmditem.Parameters.Add("@ident", item.Guid.ToString());
                cmditem.Parameters.Add("@pagename", Definition.PageName);
                dbc.ExecuteCommand(cmditem);
                i += 1;
            }
        }

        public void DeleteBlog(string PageName)
        {
            DataSource dbc = new DataSource(ContentSettings.connectionString);
            DataSourceCommand cmd = new DataSourceCommand("DELETE FROM Page WHERE pageid = @pageid; DELETE FROM BlogEntry WHERE pagename = @pageid");
            cmd.Parameters.Add("@pageid", PageName);
            dbc.ExecuteCommand(cmd);
        }

        public void AddBlogItem(string PageName, BlogEntry Item)
        {
            DataSource dbc = new DataSource(ContentSettings.connectionString);
            DataSourceCommand cmd = new DataSourceCommand("DECLARE @articlenumber int;SET @articlenumber = 0;SELECT TOP 1 @articlenumber = articlenumber FROM BlogEntry WHERE pagename = @pagename ORDER BY articlenumber DESC;SET @articlenumber=@articlenumber+1;INSERT INTO BlogEntry (pagename, ident, title, description, publishdate, author, articlenumber) VALUES (@pagename, @ident, @title, @description, @publishdate, @author, @articlenumber)");
            cmd.Parameters.Add("@pagename", PageName);
            cmd.Parameters.Add("@ident", Item.Guid.ToString());
            cmd.Parameters.Add("@title", Item.Title);
            cmd.Parameters.Add("@description", Item.Description);
            cmd.Parameters.Add("@publishdate", Item.PublishDate);
            cmd.Parameters.Add("@author", Item.Author);
            dbc.ExecuteCommand(cmd);
        }
    }
}
